<?php
date_default_timezone_set("Asia/chongqing");
error_reporting(E_ERROR);
header('Access-Control-Allow-Origin: *'); //设置http://www.baidu.com允许跨域访问
header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With'); //设置允许的跨域header
header("Content-Type: text/html; charset=utf-8");
define('_BASE_', __DIR__);
include(__DIR__.'/core/Js.php');
include(__DIR__.'/core/Images.php');
$getbaseConfig=include(__DIR__."/data/config.php");
$getconfig=file_get_contents($getbaseConfig['getConfigUrl']);
$CMSCONFIG=json_decode($getconfig,true);
//上传码验证
if(!isset($_GET['uploadtoken'])){
     echo json_encode(array(
            'state'=> 'uploadtoken参数不合法',
        ));exit;
}elseif($_GET['uploadtoken']!=md5($CMSCONFIG['uploadtoken'])){
     echo json_encode(array(
            'state'=> 'uppwd参数错误',
        ));exit;
}
$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(_BASE_."/ueditor/config.json")), true);
$action = $_GET['action'];
switch ($action) {
    case 'config':
        $result =  json_encode($CONFIG);
        break;

    /* 上传图片 */
    case 'uploadimage':
    /* 上传涂鸦 */
    case 'uploadscrawl':
    /* 上传视频 */
    case 'uploadvideo':
    /* 上传文件 */
    case 'uploadfile':
        $result = include(_BASE_."/ueditor/action_upload.php");
        break;

    /* 列出图片 */
    case 'listimage':
        $result = include(_BASE_."/ueditor/action_list.php");
        break;
    /* 列出文件 */
    case 'listfile':
        $result = include(_BASE_."/ueditor/action_list.php");
        break;

    /* 抓取远程文件 */
    case 'catchimage':
        $result = include(_BASE_."/ueditor/action_crawler.php");
        break;

    default:
        $result = json_encode(array(
            'state'=> '请求地址出错'
        ));
        break;
}
if (isset($_GET["callback"])) {
    if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
        echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
    } else {
        echo json_encode(array(
            'state'=> 'callback参数不合法'
        ));
    }
} else {
    if(isset($_POST['simpleupload_one'])){
        $url=$getbaseConfig['ueditorDomainUrl'].urlencode($result);
        echo Js::to_url($url);
    }else{
        echo $result;
    }
   
}
